﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using SZHL.UI.AppCode;
using FastReflectionLib;
using Newtonsoft.Json;
using SZHL.Data;
using System.Data;

namespace SZHL.UI.API
{
    public class TAGManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(TAGManage).GetMethod(msg.Action.ToUpper());
            TAGManage model = new TAGManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加标签
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDTAG(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            JH_Auth_TAG TAG = JsonConvert.DeserializeObject<JH_Auth_TAG>(P1);
            if (TAG.TagName.Trim() == "")
            {
                msg.ErrorMsg = "标签内容不能为空";
                return;
            }
            if (TAG.ID == 0)
            {
                TAG.CRDate = DateTime.Now;
                TAG.CRUser = UserInfo.User.UserName;
                new JH_Auth_TAGB().Insert(TAG);
            }
            else
            {
                new JH_Auth_TAGB().Update(TAG);
            }
            msg.Result = TAG;
        }
        /// <summary>
        /// 获取标签列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETTAGLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            string strSql = string.Format(" select * from JH_Auth_TAG  where CRUser='{0}'",UserInfo.User.UserName);
            if (P1 != "")
            {
                strSql += string.Format(" And Model='{0}'", P1);
            }
            if (P2 != "")
            {
                strSql += string.Format(" And TagName like '%{0}%'", P2);
            } 
            DataTable dt = new JH_Auth_TAGB().GetDTByCommand(strSql);
            msg.Result = dt;
        }
        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1">ID</param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void DELTAGBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (!new JH_Auth_TAGB().Delete(d => d.ID.ToString() == P1))
                {

                    msg.ErrorMsg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        /// <summary>
        /// 获取标签信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void GETTAGMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new JH_Auth_TAGB().GetEntity(d => d.ID.ToString() == P1);
        }
    }
}